<?php
// +----------------------------------------------------------------------
// | Author: 发仔 <i@fazi.me> 
// +----------------------------------------------------------------------

namespace app\admin\controller;

use think\Controller;
use think\Db;

class Survey extends Base
{
    protected $id = 'mot_record_id';
	protected $table = 'mot_record';
	protected $join = ['car c','c.car_id = m.car_id'];
	protected $field = 'm.*,c.number';

    //下载excel表格
    public function outexcel() {

        $P = input('param.');
        $where = [];

        $P['car_id'] && $where['car_id'] = ['=',$P['car_id']];
        $P['start_date'] && $where['start_date'] = ['>=',$P['start_date']];
        $P['end_date'] && $where['start_date'] = ['<=',$P['end_date']];//年检没有结束时间,指的应该是某个区间吧? by玉林

        $mot_record_return = Db::name('mot_record')->where($where)->whereNull('delete_time')->field('car_id,mot_add,start_date,end_date,sender,checker,price,state,company,phone,ps')->order('start_date desc')->select();
        $car_return = Db::name('car')->where('')->field('number,car_id')->select();

        if($mot_record_return) {
            echo "<table border='1'>";
            //标题
            $title = array("车牌号","年检地址","当年年检时间","下次年检时间","送检人","年检费用","检验员","年检状态","年检公司","年检联系电话","备注说明");
            //文件名
            $filename = '年检管理导出报表';
            header("Content-type:application/octet-stream");
            header("Accept-Ranges:bytes");
            header("Content-type:application/vnd.ms-excel");
            header("Content-Disposition:attachment;filename=".$filename.".xls");
            header("Pragma: no-cache");
            header("Expires: 0");
            if (!empty($title)) {
                foreach ($title as $k => $v) {
                    $title[$k] = iconv("UTF-8", "GB2312", "<td>" . $title[$k] . "</td>");
                    echo $title[$k];
                }
                echo "</tr>";
            }
            if (!empty($mot_record_return)) {
                foreach ($mot_record_return as $key => $val) {

                    if ($mot_record_return[$key]['state']=='1'){
                        $mot_record_return[$key]['state'] = "通过";
                    }
                    else{
                        $mot_record_return[$key]['state'] = "不通过";
                    }

                    foreach ($car_return as $key2 => $val2){
                        if($car_return[$key2]['car_id'] == $mot_record_return[$key]['car_id']){
                            $mot_record_return[$key]['car_id']= $car_return[$key2]['number'];
                        }
                    }

                    foreach ($val as $ck => $cv) {
                        $mot_record_return[$key][$ck] = iconv("UTF-8", "GB2312", "<td style=\"vnd.ms-excel.numberformat:@\">" . $mot_record_return[$key][$ck] . "</td>");
                        echo $mot_record_return[$key][$ck];
                    }
                    echo "</tr>";
                }
            }
        }
        else {
            $this->error('无数据');
        }
    }
}
